<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.view' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardViewJAApplications extends JView
{
    /**
     * Display the view
     */
    function display($tmpl = null){
        global $mainframe;
        $title = getPageTitleFromMenu();
    	$config = new JConfig();
		$document = JFactory::getDocument();      
        $user = &JFactory::getUser();
        $backend_usertype = array(JBADMIN_USER, JBSUPERADMIN);
		
        if (getCurrentUserType() != JBEMPLOYER_USER && getCurrentUserType() != JBJOB_SEEKER_USER && !in_array($user->usertype,$backend_usertype)) {
            JResponse::setHeader('HTTP/1.0 403',true);
            JError::raiseWarning( 403, JText::_("Access denied. You don't have permission.") ); 
            echo "<a href='javascript:window.history.go(-1);'>". JText::_('Back')."</a>"; 
            return;
        }
		if(getCurrentUserType() == JBEMPLOYER_USER){
			$this->setLayout('jaempllist');
			JRequest::setVar('layout', 'jaempllist');
		}
    	switch ($this->getLayout()){
    		
    		case 'my-applications':
    			$this->show_list();
    			if (!$title)
					$title = JText::_('Application Manager');
    			break;
    			
    		case 'jaempllist':    			
    			$this->show_list_for_employer();
    			if (!$title)
					$title = JText::_('Application Manager');
				$document->setTitle($title.' - '.$config->sitename);
    			break;
    		
    		case 'jaview':
    			$this->show_application_details();
    			break;    			    		    		
    			
    	}
        parent::display($tmpl);
  	}
    
  	function show_list()
  	{
  		
  		checkPermission('view_list_applications');
  		global $mainframe, $jbconfig, $option;
  		// get data items
        $model = &$this->getModel();               
		$user = JFactory::getUser();              
        $limitstart = JRequest::getInt('limitstart', 0);
		$limit		= $mainframe->getUserStateFromRequest( $option.'.jaresumes.limit',		'limit',	$jbconfig['posts']->get('display_num', 20),	'int' );
		$where_more = '';
		
		if($this->getLayout()=='my-applications'){
			$where_more = " and `user_id`='$user->id'";
			$where_more .= "  and (`params`  NOT LIKE '%jobseeker_removed=1%' or  `params`  IS NULL) ";
		}		
			
       	$items = $model->getItems($where_more, $limit, $limitstart);       	
       	
       	$fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0');
       	$fieldList = $model->getFieldList();
		
		$pagination = &$this->get('Pagination');       
        
        $lists = $this->_getViewLists($fieldEditList);        
        
        $ordering = true;      
        $this->assignRef('items', $items);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('lists', $lists);
        $this->assignRef('ordering', $ordering );
                
  	}
  	
    function show_list_for_employer(){
    	checkPermission('view_list_applications');
        global $mainframe, $jbconfig, $option;
  		// get data items
        $model = &$this->getModel();               
        $user = JFactory::getUser();
        
        $limitstart = JRequest::getInt('limitstart', 0);
		$limit		= $mainframe->getUserStateFromRequest( $option.'.jaapplications.limit',		'limit',	5,	'int' ); 
        
        $this->assign('myapplication', 1);               
		
        $items = $model->getListItem_Employer($user->id,$limit,$limitstart);  
        
       	$fieldEditList = $model->getEditFieldList(' and f.frontend_hidden=0');
       	$fieldList = $model->getFieldList();
		
		$pagination = &$this->get('Pagination');       
        
        $lists = $this->_getViewLists($fieldEditList);
        
        $ordering = true;      
        $this->assignRef('items', $items);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('lists', $lists);
        $this->assignRef('ordering', $ordering );
        
        $Itemid = JRequest::getVar('Itemid');
        $this->assign('Itemid', $Itemid);
      }
  
  	/**
  	* Display edit form
  	*/
  	function edit(){
        $item = $this->get('Item');
        if($item->id)
            JRequest::setVar('job_id', $item->job_id);
            
        $job = $this->get('JobInfo');
        $resumes = $this->get('Resumes');
        $fieldList = $this->get('EditFieldList');
        $Itemid = JRequest::getVar('Itemid');
       
        $this->assignRef('item', $item);
        $this->assignRef('job', $job);
        $this->assignRef('resumes', $resumes);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('Itemid', $Itemid);
        parent::display();
    }
    
    /**
      * Display view form
      */
    function show_application_details(){
        $Itemid = JRequest::getVar('Itemid');       
        $fromsearch = JRequest::getVar('fromsearch', 0);
        
        $item = $this->get('Item');
        JRequest::setVar('job_id', $item->job_id);
        JRequest::setVar('resume_id', $item->resume_id);
        
        $job = $this->get('JobInfo');
        $resumes = $this->get('Resumes');
        $fieldList = $this->get('ViewFieldList');
        
        $this->assignRef('item', $item);
        $this->assignRef('job', $job); 
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('Itemid', $Itemid);
        $this->assignRef('fromsearch', $fromsearch);         
        parent::display();
    }
    
    /**
      * Display edit form
      */
    function viewothers(){
        $item = $this->get('Item');
        $fieldList = $this->get('EditFieldList');

        if($item->id > 0){
            JToolBarHelper::cancel('cancel', 'Close');    
        } else {
            JToolBarHelper::cancel();    
        }
        $toolbar = &JToolBar::getInstance('toolbar');
        $Itemid = JRequest::getVar('Itemid');
        
        $this->setLayout(JBPREFIX."view");
        
        $this->assignRef('toolbar', $toolbar);
        $this->assignRef('item', $item);
        $this->assignRef('fieldlist', $fieldList);
        $this->assignRef('Itemid', $Itemid);
        parent::display();
    }
    
	function &_getViewLists(){
		global $mainframe;

		$db = &JFactory::getDBO();
        
        $filter_order		= $mainframe->getUserStateFromRequest( JBCOMNAME.".jobs.filter_order",		'filter_order',		'',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( JBCOMNAME.".jobs.filter_order_Dir",	'filter_order_Dir',	'ASC',			'word' );
		
		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;

		return $lists;
	}

	function getToggleLink( $field_name, $row,$check=false, $imgY = 'Favorites-16x16.png', $imgX = 'favorite-remove-16x16.png', $prefix=''){
    	global $mainframe;
    	$img     = $check ? $imgY : $imgX;
        $task = $check ? "inactivate":"activate";
        $alt     = $check ? JText::_( 'REMOVE FAVORITE' ) : JText::_( 'SET AS FAVORITE' );
        $action  = $check ? JText::_( 'REMOVE FAVORITE' ) : JText::_( 'SET AS FAVORITE' );
                
        $href = '
        <a id = "hrf_'.$row->id.'" href="javascript:void(0);" onclick="return listItemTask(\''. $row->id .'\',\''. $field_name. '\',\''.$prefix.$task .'\')" title="'. $action .'">
        <img  id = "img_'.$row->id.'" src="'.$mainframe->getCfg('live_site').'/components/com_jajobboard/images/'. $img .'" border="0" alt="'. $alt .'" /></a>'
        ;
        return $href;
    } 

}
?>